package cn.wenhe9.gulimall.product.service.impl;

import cn.wenhe9.gulimall.product.dao.AttrAttrgroupRelationDao;
import cn.wenhe9.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.wenhe9.gulimall.product.entity.AttrGroupEntity;
import cn.wenhe9.gulimall.product.service.AttrAttrgroupRelationService;
import cn.wenhe9.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.wenhe9.gulimall.product.dao.AttrDao;
import cn.wenhe9.gulimall.product.entity.AttrEntity;
import cn.wenhe9.gulimall.product.service.AttrService;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Resource
    private AttrAttrgroupRelationService relationService;

    @Override
    public IPage<AttrEntity> queryPage(Map<String, Object> params) {
        Long currnet = (Long) params.get("page");
        Long limit = (Long) params.get("limit");
        IPage<AttrEntity> page = this.page(
                new Page<AttrEntity>(currnet, limit),
                new QueryWrapper<AttrEntity>()
        );
        return page;
    }

    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        //保存基本数据
        this.save(attrEntity);
        //保存关联关系
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        relationService.save(relationEntity);
    }

}